<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="form.le_id ? '编辑授权接口' : '添加授权接口'" v-model="visible" append-to-body width="800px"
    @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top">
      <div class="grid grid-cols-7 gap-x-3">
        <el-form-item label="平台" prop="platform_id" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]" class="col-span-6">
          <el-radio-group v-model="form.platform_id" :disabled="!!form.le_id">
            <el-radio v-for="(d, i) in options.platform" :key="i" :label="d.value">{{ d.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.status" inline-prompt active-text="启用" inactive-text="禁用" :active-value='2'
            :inactive-value='1' />
        </el-form-item>
      </div>
      <div class="grid grid-cols-2 gap-x-3">
        <el-form-item label="错误代码" prop="error_code" :rules="[{ required: true, message: '不能为空', trigger: 'blur' }]">
          <el-input v-model="form.error_code" placeholder="请输入" class="w-full" />
        </el-form-item>
        <el-form-item label="错误类型" prop="type" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <VSelect v-model="form.type" placeholder="请选择" :options="options.type" class="w-full" />
        </el-form-item>
      </div>
      <el-form-item label="关键词">
        <el-input v-model="form.keyword" placeholder="请输入" class="w-full" />
      </el-form-item>
      <el-form-item label="英文说明" prop="en_explain" :rules="[{ required: true, message: '不能为空', trigger: 'blur' }]">
        <el-input v-model="form.en_explain" placeholder="请输入" :maxlength="250" show-word-limit />
      </el-form-item>
      <el-form-item label="中文说明">
        <el-input v-model="form.cn_explain" placeholder="请输入" :maxlength="250" show-word-limit />
      </el-form-item>
      <el-form-item label="解决方案" prop="solution" :rules="[{ required: true, message: '不能为空', trigger: 'blur' }]">
        <el-input v-model="form.solution" placeholder="请输入" type="textarea" :autosize="{ minRows: 2, maxRows: 5 }"
          :maxlength="800" show-word-limit />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.remark" type="textarea" :autosize="{ minRows: 2, maxRows: 5 }" placeholder="请输入"
          :maxlength="800" show-word-limit />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">关 闭</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'

const props = defineProps({
  options: { type: Object, default: () => ({}) },
})

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})
const formRef = ref()

const emit = defineEmits(['close', 'success'])

// 打开弹窗
const open = async (le_id) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  loadings = reactive({})
  visible.value = true
  if (le_id) {
    const res = await api.detail({ le_id })
    form.value = res.data
    formRef?.value?.clearValidate()
  } else {
    form.value = { platform_id: 1, status: 1 }
    formRef?.value?.resetFields()
  }
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = async () => {
  const validator = await formRef?.value.validate()
  if (!validator) return
  loadings.submit = true
  api[form.value.le_id ? 'edit' : 'create'](form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>